package sunyu.kit.common;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符串工具类
 *
 * @author 孙宇
 */
public class StringKit {

    private final static char[] hexChars = "0123456789ABCDEF".toCharArray();

    /**
     * 判断是否为空
     *
     * @param cs
     * @return
     */
    public static boolean isBlank(CharSequence cs) {
        return StringUtils.isBlank(cs);
    }

    /**
     * 判断是否不为空
     *
     * @param cs
     * @return
     */
    public static boolean isNotBlank(CharSequence cs) {
        return StringUtils.isNotBlank(cs);
    }

    /**
     * 去左右空格
     *
     * @param str
     * @return
     */
    public static String trim(String str) {
        return StringUtils.trim(str);
    }


    /**
     * 将给定的字节数组转换为16进制字符串形式
     *
     * @return
     */
    public static String bytes2hexString(final short[] bytes) {
        StringBuffer strBuf = new StringBuffer();
        if (bytes != null && bytes.length > 0) {
            for (Short data : bytes) {
                strBuf.append(String.format("%02X", data));
            }
        }
        return strBuf.toString();
    }

    /**
     * 将给定的字节数组转换为16进制字符串形式
     *
     * @return
     */
    public static String bytes2hexString(final byte[] bytes) {
        StringBuffer strBuf = new StringBuffer();
        if (bytes != null && bytes.length > 0) {
            for (byte data : bytes) {
                strBuf.append(String.format("%02X", data));
            }
        }
        return strBuf.toString();
    }

    /**
     * 字符串转16进制字符串
     *
     * @param str
     * @return
     */
    public static String string2hexString(String str) {
        StringBuilder sb = new StringBuilder();
        byte[] bs = str.getBytes();
        for (int i = 0; i < bs.length; i++) {
            sb.append(hexChars[(bs[i] & 0xFF) >> 4]);
            sb.append(hexChars[bs[i] & 0x0F]);
        }
        return sb.toString().trim();
    }

    /**
     * 16进制字符串转字符串
     *
     * @param hexStr
     * @return
     */
    public static String hexString2String(String hexStr) {
        hexStr = hexStr.toUpperCase();
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    /**
     * 替换字符串的一部分
     *
     * @param source        源字符串
     * @param start         开始索引
     * @param stop          结束索引
     * @param replaceString 要替换的字符串
     * @return 替换后的字符串
     */
    public static String replaceRange(String source, Integer start, Integer stop, String replaceString) {
        StringBuffer newString = new StringBuffer();
        if (start != null) {
            newString.append(source.substring(0, start));
        }
        newString.append(replaceString);
        if (stop != null) {
            newString.append(source.substring(stop));
        }
        return newString.toString();
    }

}
